package com.lxmes.device.controller;

import com.lxmes.common.annotation.Log;
import com.lxmes.common.core.controller.BaseController;
import com.lxmes.common.core.domain.AjaxResult;
import com.lxmes.common.core.page.TableDataInfo;
import com.lxmes.common.enums.BusinessType;
import com.lxmes.common.utils.poi.ExcelUtil;
import com.lxmes.device.domain.NcEmJggx;
import com.lxmes.device.service.INcEmJggxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 加工程序存储Controller
 *
 * @author ruoyi
 * @date 2023-02-10
 */
@RestController
@RequestMapping("/device/jggx")
public class NcEmJggxController extends BaseController
{
    @Autowired
    private INcEmJggxService ncEmJggxService;

    /**
     * 查询加工程序存储列表
     */
    @PreAuthorize("@ss.hasPermi('device:jggx:list')")
    @GetMapping("/list")
    public TableDataInfo list(NcEmJggx ncEmJggx)
    {
        startPage();
        List<NcEmJggx> list = ncEmJggxService.selectNcEmJggxList(ncEmJggx);
        return getDataTable(list);
    }

    /**
     * 导出加工程序存储列表
     */
    @PreAuthorize("@ss.hasPermi('device:jggx:export')")
    @Log(title = "加工程序存储", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, NcEmJggx ncEmJggx)
    {
        List<NcEmJggx> list = ncEmJggxService.selectNcEmJggxList(ncEmJggx);
        ExcelUtil<NcEmJggx> util = new ExcelUtil<NcEmJggx>(NcEmJggx.class);
        util.exportExcel(response, list, "加工程序存储数据");
    }

    /**
     * 获取加工程序存储详细信息
     */
    @PreAuthorize("@ss.hasPermi('device:jggx:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(ncEmJggxService.selectNcEmJggxById(id));
    }

    /**
     * 新增加工程序存储
     */
    @PreAuthorize("@ss.hasPermi('device:jggx:add')")
    @Log(title = "加工程序存储", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody NcEmJggx ncEmJggx)
    {
        return toAjax(ncEmJggxService.insertNcEmJggx(ncEmJggx));
    }

    /**
     * 修改加工程序存储
     */
    @PreAuthorize("@ss.hasPermi('device:jggx:edit')")
    @Log(title = "加工程序存储", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody NcEmJggx ncEmJggx)
    {
        return toAjax(ncEmJggxService.updateNcEmJggx(ncEmJggx));
    }

    /**
     * 删除加工程序存储
     */
    @PreAuthorize("@ss.hasPermi('device:jggx:remove')")
    @Log(title = "加工程序存储", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(ncEmJggxService.deleteNcEmJggxByIds(ids));
    }

    /**
     * 导入加工程序存储
     */
    @Log(title = "加工程序存储", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<NcEmJggx> util = new ExcelUtil<>(NcEmJggx.class);
        List<NcEmJggx> ncEmJggxList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = ncEmJggxService.imporIteminfo(ncEmJggxList, updateSupport, operName);
        return AjaxResult.success(message);
    }
    // 下载模板
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException
    {
        ExcelUtil<NcEmJggx> util = new ExcelUtil<>(NcEmJggx.class);
        util.importTemplateExcel(response,"加工程序存储");
    }
}
